package com.simple.util.math.function;


import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @program: simple_tools
 * @description: 数学(函数圆)
 * @author: ChenWenLong
 * @create: 2019-10-24 17:38
 **/
public class CircleFunction {

    //圆心点
    private static Point centerPoint = new Point(0,0);
    //默认半径
    private double r = 1;
    private static CircleFunction instance;
    private CircleFunction(){}
    private static double distance = 0;
    //在圆上
    private static final int ONSIDE_CIRCLE = 0;
    //在圆内
    private static final int INSIDE_CIRCLE = 1;
    //在圆外
    private static final int OUTSIDE_CIRCLE = 2;
    //求距离
    private static final int DISTANCE_CENTER_POINT = 3;

    //默认创建一个圆心在原点的圆
    static {
        if(instance == null){
            synchronized (CircleFunction.class){
                if(instance == null){
                    instance = new CircleFunction();
                }
            }
        }
    }

    /**
     * 功能描述:
     * 〈初始化一个原点为中心,r为半径的圆〉
     *
     * @params : [r]
     * @return : void
     * @author : cwl
     * @date : 2019/10/24 17:46
     */
    public static void init(double r){
        instance.setR(r);
    }

    /**
     * 功能描述:
     * 〈初始化一个半径为r,圆心为point的圆〉
     *
     * @params : [r, point]
     * @return : void
     * @author : cwl
     * @date : 2019/10/24 17:47
     */
    public static void init(double r,Point point){
        instance.setR(r);
        instance.setcenterPoint(point);
    }

    /**
     * 功能描述:
     * 〈判断点是否在圆上〉
     *  (x-a)^2 + (y - b)^2 = r^2
     *
     * @params : [point]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 17:51
     */
    public static boolean isOnsideCircle(Point point){
        return handleCompare(point,ONSIDE_CIRCLE);
    }

    /**
     * 功能描述:
     * 〈处理圆上/圆内/圆外的比较〉
     *
     * @params : [point, type]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 18:15
     */
    private static boolean handleCompare(Point point, int type) {
        if(point == null){
            throw new RuntimeException("point is not be null !");
        }
        Point centerPoint = instance.getcenterPoint();
        double r = instance.getR();
        double a = centerPoint.getX();
        double b = centerPoint.getY();
        double x = point.getX();
        double y = point.getY();
        switch(type){
            case ONSIDE_CIRCLE:
                return Math.pow(2,r) == Math.pow(2,x-a) + Math.pow(2,y-b);
            case INSIDE_CIRCLE:
                return Math.pow(2,r) >= Math.pow(2,x-a) + Math.pow(2,y-b);
            case OUTSIDE_CIRCLE:
                return Math.pow(2,r) <= Math.pow(2,x-a) + Math.pow(2,y-b);
            case DISTANCE_CENTER_POINT:
                distance = new BigDecimal(Math.sqrt(Math.abs(Math.pow(2,x-a) + Math.pow(2,y-b)))).setScale(2, RoundingMode.HALF_UP).doubleValue();
                return true;
            default:
                return false;
        }
    }

    /**
     * 功能描述:
     * 〈判断点是否在圆内〉
     *  (x-a)^2 + (y - b)^2 = r^2
     *
     * @params : [point]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 18:01
     */
    public static boolean isInsideCircle(Point point){
        return handleCompare(point,INSIDE_CIRCLE);
    }

    /**
     * 功能描述:
     * 〈判断点是否在圆外〉
     *
     * @params : [point]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 18:17
     */
    public static boolean isOutsideCircle(Point point){
        return handleCompare(point,OUTSIDE_CIRCLE);
    }

    /**
     * 功能描述:
     * 〈获得点到圆心的距离〉
     *
     * @params : [point]
     * @return : double
     * @author : cwl
     * @date : 2019/10/24 19:34
     */
    public static double getCenterDistance(Point point){
        if(point == null){
            throw new RuntimeException("point is not be null!");
        }
        double result ;
        if(handleCompare(point,DISTANCE_CENTER_POINT)){
            result = distance;
            // 恢复距离的初始化
            distance = 0;
            return result;
        }
        return distance;
    }

    //点
    public static class Point{
        // x坐标
        private double x;
        // y坐标
        private double y;

        private Point(){};
        public Point(double x,double y){
            this.x = x;
            this.y = y;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    public Point getcenterPoint() {
        return centerPoint;
    }

    public void setcenterPoint(Point centerPoint) {
        this.centerPoint = centerPoint;
    }
}
